package com.futvan.z.erp.customer_notice;

import com.futvan.z.framework.common.bean.Result;
import com.futvan.z.framework.core.z;
import com.futvan.z.framework.util.HttpUtil;
import com.futvan.z.framework.util.TicketExcelUtil;
import org.springframework.stereotype.Controller;
import com.futvan.z.framework.core.SuperAction;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Controller
public class Customer_noticeAction extends SuperAction {
    @Resource
    private Customer_noticeService customer_noticeService;


    /**
     * 发送通知
     *
     * @param url     文件下载地址
     * @param type    发送类型
     * @param title   发送标题
     * @param content 发送内容
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/customerNoticeSend")
    public @ResponseBody
    Result sendNotice(String url, String type, String title, String content) throws Exception {
        //下载到本地
        String path = HttpUtil.download(url, System.getProperty(z.sp.get("tempfiles")) + "/temp/customerExcel", "customerExcel");
        //解析excel
        List<crm_customer_notice_bean> customerList = TicketExcelUtil.parseFromExcel(path, 1, crm_customer_notice_bean.class);
        //去除重复数据
        List<String> custom = new ArrayList<>();
        for (int i = 0; i < customerList.size(); i++) {
            custom.add(customerList.get(i).getCustomer_id());
        }
        List<String> collect = Stream.of(custom).flatMap(Collection::stream).distinct().collect(Collectors.toList());
        if (z.isNotNull(collect) && collect.size() > 0) {
            return customer_noticeService.send(collect, type, title, content, url);
        } else {
            return Result.error("没有有效用户");
        }

    }


    @RequestMapping(value = "/customerNoticeMessageSend")
    public @ResponseBody
    Result sendNoticeMessage(String url, String templateCode) throws Exception {
        //下载到本地
        String path = HttpUtil.download(url, System.getProperty(z.sp.get("tempfiles")) + "/temp/customerExcel", "customerExcel");
        //解析excel
        List<crm_customer_notice_bean> customerList = TicketExcelUtil.parseFromExcel(path, 1, crm_customer_notice_bean.class);
        //去除重复数据
        List<String> custom = new ArrayList<>();
        for (int i = 0; i < customerList.size(); i++) {
            custom.add(customerList.get(i).getCustomer_id());
        }
        List<String> collect = Stream.of(custom).flatMap(Collection::stream).distinct().collect(Collectors.toList());
        if (z.isNotNull(collect) && collect.size() > 0) {
            return customer_noticeService.sendMessage(collect, templateCode, url);
        } else {
            return Result.error("没有有效用户");
        }
    }
}
